package com.elitesland.system.rest;

import com.elitesland.core.base.ApiCode;
import com.elitesland.core.base.ApiResult;
import com.elitesland.core.base.PagingVO;
import com.elitesland.system.param.SysNextNumberQParam;
import com.elitesland.system.service.SysNextNumberService;
import com.elitesland.system.vo.SysNextNumberVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <pre></pre>
 *
 * @author mir
 * @date 2020-10-12
 */
@RestController
@RequestMapping("/sys/numbering/nn")
@Api(value = "下一编号管理", tags = {"发号器API"})
public class SysNextNumberController {

    private SysNextNumberService sysNextNumberService;

    @Autowired
    public void setSysNextNumberService(SysNextNumberService sysNextNumberService) {
        this.sysNextNumberService = sysNextNumberService;
    }

    @PostMapping("/q")
    @ApiOperation("检索下一编号")
    public ApiResult<PagingVO<SysNextNumberVO>> search(@RequestBody SysNextNumberQParam param) {
        val ret = sysNextNumberService.search(param);
        return ApiResult.ok(ret);
    }

    @GetMapping("/{id}")
    @ApiOperation("根据ID获取对应下一编号")
    public ApiResult<SysNextNumberVO> oneById(@PathVariable Long id) {
        return sysNextNumberService.oneById(id).map(ApiResult::ok)
                .orElse(ApiResult.fail(ApiCode.NOT_FOUND));
    }

    @PostMapping
    @ApiOperation("创建一个新的下一编号")
    public ApiResult<Long> create(@RequestBody SysNextNumberVO nextNumber) {
        val id = sysNextNumberService.create(nextNumber);
        return ApiResult.ok(id);
    }

    @PutMapping
    @ApiOperation("更新下一编号配置")
    public ApiResult<Object> update(@RequestBody SysNextNumberVO nextNumber) {
        sysNextNumberService.update(nextNumber);
        return ApiResult.ok();
    }

    @DeleteMapping
    @ApiOperation("根据ID列表，删除对应的下一编号")
    public ApiResult<Object> deleteByIds(@RequestBody List<Long> ids) {
        sysNextNumberService.deleteByIds(ids);
        return ApiResult.ok();
    }
}
